#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Introduction to Programming in Java - An Interdisciplinary Approach

All book programs ported to Python by
Scott C. King (revisionx at {gee}ma1l <dot> c0m).

All program examples pass Pylint with a perfect 10.00 score.
Pylint:  http://www.pylint.org/


Program 1.5.5 - Input-to-drawing filter

Syntax:
python plotfilter.py

Alternate (see notes below):
python plotfilter.py < USA.txt


Notes:
sys is a required import here in order to use stdin/sys.argv.
https://docs.python.org/2/tutorial/stdlib.html

Turtle is a required import here for a comparable Python module.
https://docs.python.org/2/library/turtle.html

Turtle is, well, considerably slower than the StdDraw module in Java.

The default scaling parameters in USA.txt created a distorted image with
turtle.Screen's setworldcoordinates method, so I tweaked the ratios.

To load from stdin, follow the comments in the code below.  If using
stdin, the turtle image will close the instant it completes plotting,
thus the decision to load from a file.

   for coordinate in sys.stdin:
       temp = coordinate.strip().split()

'''

# import sys                                    # For stdin, import sys
import turtle


def main():
    ''' Plot points of USA.txt using Python's turtle module. '''

    my_turtle = turtle.Turtle()
    my_screen = turtle.Screen()

    with open('USA.txt') as my_file:            # To pipe via stdin, remove or
        coordinates = my_file.readlines()       # comment out these two lines.

    for coordinate in coordinates:              # Replace with lines in notes
        temp = coordinate.strip().split()       # to pipe via stdin.

        if len(temp) != 4:
            # Read and plot remaining points
            my_turtle.setposition(float(temp[0]), float(temp[1]))
            my_turtle.pendown()
            my_turtle.dot(size=7)
            my_turtle.penup()

        else:
            # Use bounding box coordinates from first line in USA.txt
            my_screen.setworldcoordinates(float(temp[0]), float(temp[1]),
                                          float(temp[2]), float(temp[3]))
            my_screen.title('Plot Filter')
            my_turtle.speed(0)
            my_turtle.hideturtle()
            my_turtle.penup()


if __name__ == '__main__':
    main()
